package com.lezheba.service;

import java.util.Date;

import com.lezheba.db.Dao;
import com.lezheba.db.entity.DaythInfo;
import com.lezheba.db.entity.User;

public class UserAdminService {
	private Dao dao = Dao.getInstance();

	public boolean isExist(String email) {
		return get(email) != null;
	}

	public User get(String email) {
		String hql = "from User where email=?";
		return (User) dao.getObjectByHql(hql, email);
	}

	public User get(String email, String password) {
		String hql = "from User where email=? and password=?";
		return (User) dao.getObjectByHql(hql, new Object[] { email, password });
	}

	public DaythInfo get(int dayth) {
		String hql = "from DaythInfo where dayth=?";
		return (DaythInfo) dao.getObjectByHql(hql, Long.valueOf(dayth));
	}

	public void saveOrUpdate(int dayth, String path, String title,
			String content) {
		DaythInfo daythInfo = get(dayth);
		if (daythInfo == null) {
			daythInfo = new DaythInfo();
		}
		daythInfo.setDayth(new Long(dayth));
		daythInfo.setPath(path);
		daythInfo.setTitle(title);
		daythInfo.setContent(content);
		Dao.getInstance().saveOrUpdate(daythInfo);
		System.out.println(daythInfo.toString());
	}

	public void addUser(String email, String password, Date childBirth) {
		User newUser = new User();
		newUser.setEmail(email);
		newUser.setPassword(password);
		newUser.setChildBirth(childBirth);
		Dao.getInstance().save(newUser);
	}

}
